#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/input.h>
#include <tslib.h>

int main(int argc, char *argv[]) 
{
    struct tsdev *ts = NULL;
    struct ts_sample_mt *mt_ptr = NULL;
    struct input_absinfo slot;
    int max_slots;
    unsigned int pressure[12] = {0};
    int i;

    /* 打开并配置触摸屏 */
    ts = ts_setup(NULL, 0); 
    if (ts == NULL) {
        fprintf(stderr, "ts_read error");
        exit(EXIT_FAILURE);
    }

    /* 获取触摸屏最大触摸点数 */
    if (ioctl(ts_fd(ts), EVIOCGABS(ABS_MT_SLOT), &slot) < 0) {
        perror("ioctl error");
        ts_close(ts);
        exit(EXIT_FAILURE);
    }

    max_slots = slot.maximum + 1 - slot.minimum;
    printf("max_slots: %d\n", max_slots);

    /* 分配内存 */
    mt_ptr = calloc(max_slots, sizeof(struct ts_sample_mt));

    /* 读数据 */
    while (1) {
        if (ts_read_mt(ts, &mt_ptr, max_slots, 1) < 0) {
            perror("ts_read_mt error");
            ts_close(ts);
            exit(EXIT_FAILURE);
        }

        for (i=0; i<max_slots; i++) {
            if (mt_ptr[i].valid) {      // 有效更新
                if (mt_ptr[i].pressure) {   // 有按压
                    if (pressure[mt_ptr[i].slot] > 0) {
                        printf("slot<%d>, 移动(%d, %d)\n", mt_ptr[i].slot, mt_ptr[i].x, mt_ptr[i].y);
                    } else {
                        printf("slot<%d>, 按下(%d, %d)\n", mt_ptr[i].slot, mt_ptr[i].x, mt_ptr[i].y);
                    }      
                } else {
                    printf("slot<%d>, 松开\n", mt_ptr[i].slot);
                }

                pressure[mt_ptr[i].slot] = mt_ptr[i].pressure;
            }
        }
    }

    ts_close(ts);
    free(mt_ptr);       // 必须要有
    exit(EXIT_SUCCESS);
}